﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Adding
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_ContactAR : System.Web.UI.UserControl
    {
        #region Fields

        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_FIM_MAS_Contacts con = new B_FIM_MAS_Contacts();

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ddlContactSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblContactSeq.Text = ddlContactSeq.SelectedValue;

            if (Child != null)
            {
                Child.Text = "";

                if (ddlContactSeq.SelectedValue.Length <= 0)
                {
                    Child.DataSource = null;
                    Child.DataBind();
                }
            }
        }

        protected void ddlContactSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }

        #endregion

        #region Public Methods

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlContactSeq;
            return ddl;
        }        

        public void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strName = lblName.Text;
            string strValue = lblValue.Text;
            string strFilter = e.Text;
            IList<M_FIM_MAS_Contacts> infolist = null;

            infolist = con.GetListAR(strFilter);                    

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;
                        
            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlContactSeq.Items.Add(new RadComboBoxItem(infolist[i].contact_name, infolist[i].contact_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset
            , string strName, string strValue)
        {
            if (nWidth != null)
                ddlContactSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblName.Text = strName;
            lblValue.Text = strValue;
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset)
        {
            LoadControl(nWidth, nSetOffset, "", "");
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue == null)
            {
                strValue = "";
            }

            if (strValue.Length > 0)
            {
                lblContactSeq.Text = strValue;

                if (ddlContactSeq.FindItemByValue(strValue) == null)
                {
                    M_FIM_MAS_Contacts info = con.GetInfo(strValue);
                    ddlContactSeq.Text = info.contact_name;
                }
                else
                {
                    ddlContactSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlContactSeq.SelectedIndex = -1;
                ddlContactSeq.Text = "";
                ddlContactSeq.Text = "";
            }
        }

        public string GetSelectedValue()
        {
            string strValue = lblContactSeq.Text;

            return strValue;
        }

        #endregion
    }
}